/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.shop.api.entity.GoodsLabel;
import com.pig4cloud.pigx.shop.service.GoodsLabelService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 商品标签
 *
 * @author pig code generator
 * @date 2023-09-07 22:34:19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goods/label")
@Tag(name = "商品标签管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsLabelController {

	private final GoodsLabelService goodsLabelService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param goodsLabel 商品标签
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getGoodsLabelPage(Page page, GoodsLabel goodsLabel) {
		LambdaQueryWrapper<GoodsLabel> lqw = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(goodsLabel.getLabelName())) {
			lqw.like(GoodsLabel::getLabelName, goodsLabel.getLabelName());
		}
		lqw.orderByDesc(GoodsLabel::getCreateTime);
		return R.ok(goodsLabelService.page(page, lqw));
	}

	/**
	 * 通过id查询商品标签
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(goodsLabelService.getById(id));
	}

	/**
	 * 新增商品标签
	 * @param goodsLabel 商品标签
	 * @return R
	 */
	@Operation(summary = "新增商品标签", description = "新增商品标签")
	@SysLog("新增商品标签")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('goods_label_add')")
	public R save(@RequestBody GoodsLabel goodsLabel) {
		return R.ok(goodsLabelService.save(goodsLabel));
	}

	/**
	 * 修改商品标签
	 * @param goodsLabel 商品标签
	 * @return R
	 */
	@Operation(summary = "修改商品标签", description = "修改商品标签")
	@SysLog("修改商品标签")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('goods_label_edit')")
	public R updateById(@RequestBody GoodsLabel goodsLabel) {
		return R.ok(goodsLabelService.updateById(goodsLabel));
	}

	/**
	 * 通过id删除商品标签
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除商品标签", description = "通过id删除商品标签")
	@SysLog("通过id删除商品标签")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('goods_label_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(goodsLabelService.removeById(id));
	}

	/**
	 * 获取所有标签
	 * @return
	 */
	@GetMapping("/list")
	public R getGoodsLabelList() {
		LambdaQueryWrapper<GoodsLabel> lqw = new LambdaQueryWrapper<>();
		lqw.eq(GoodsLabel::getDelFlag, DeleteFlagEnum.VALID.getType());
		lqw.orderByDesc(GoodsLabel::getCreateTime);
		return R.ok(goodsLabelService.list(lqw));
	}

}
